home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Eagles Nest BBS 8
/
Eagles_Nest_Mac_Collection_Disc_8.TOAST
/
Developer Tools⁄Additions
/
IntermediatC
/
Ships 2 #8
/
ships 2.c
next >
Wrap
C/C++ Source or Header
|
1990-11-02
|
7KB
|
237 lines
/*
GIVEN:
#include <math.h>
#define RADIANS_PER_DEGREE 0.017453292519943
typedef struct ship_type
{
int hull_number;
position_type posit;
double course;
double speed;
} ship_type;
CREATE THIS PROGRAM:
Create NO_OF_SHIPS (define anywhere from 10 to 20) at random positions -100 to 100
(longitude and latitude will be of type double) with randomly assigned courses from
0 up to and including 359 degrees and speed from 10 to 30 knots. North is 0 degrees,
and degrees increase clockwise (a diagram is provided below). Assign the hull number
any way you desire, but all ships must have a different hull number. Display all
ship information. This includes the hull number, position (longitude and latitude),
course, and speed.
Now, let the ships sail (they move the assigned speed in the assigned direction).
Ignore units (thus, a ship at 0 longitude and 0 latitude with a course of 180 and
speed of 15 would move 15 south to 0 longitude and -15 latitude). The ships are to
each move 5 times. After each set of moves, eliminate any ship that is no longer in
the region of -100 to 100 longitude and -100 to 100 latitude. Thus NO_OF_SHIPS are
to move, the program will search to see if any ships are no longer in the boundaries,
eliminate tracking those ships in the future, and proceed to the next set of movements.
As said earlier, the ships each move 5 times (unless no longer being tracked).
Those ships still in the boundaries after 5 moves are to be listed as above in the same
file.
DIAGRAM OF DIRECTIONS
North
0 degrees
|
|
|
West 270 degrees --------- 90 degrees East
|
|
|
180 degrees
South
*/
/* ------------------------------------------------------------------------------- */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* ------------------------------------------------------------------------------- */
#define random(x,y) (rand() % ((y) - (x) + 1) + (x))
#define RADIANS_PER_DEGREE 0.017453292519943 /* conversion factor for degrees to radians */
#define TRACKING 0xff
#define NOT_TRACKING 0x00
#define NO_OF_SHIPS 12 /* maximum number of ships */
#define SHIP_HOURS 5 /* number of times ships move */
#define MIN_POSITION -100 /* min and max positions, courses and speeds */
#define MAX_POSITION 100
#define MIN_COURSE 0
#define MAX_COURSE 359
#define MIN_SPEED 10
#define MAX_SPEED 30
#define INITIAL_SHIP_INFO 0xff /* used to determine header of output file */
#define FINAL_SHIP_INFO 0x00
/* ------------------------------------------------------------------------------- */
typedef struct position_type
{
double longitude;
double latitude;
} position_type;
typedef struct ship_type
{
int hull_number;
position_type posit;
double course;
double speed;
} ship_type;
/* ------------------------------------------------------------------------------- */
static void initialize_ships( ship_type ships[], char tracking[], int no_of_ships );
static void move_ships( ship_type ships[], char tracking[], int no_of_ships );
static void display_ship_info( ship_type ships[], char tracking[],
int no_of_ships, FILE *file_stream, char output_type );
static void find_new_position( ship_type ships[], int sub );
static int check_if_still_track( ship_type ships[], int sub );
/* ------------------------------------------------------------------------------- */
main()
{
ship_type ships[NO_OF_SHIPS];
char tracking[NO_OF_SHIPS];
int i;
FILE *file_stream;
initialize_ships( ships, tracking, NO_OF_SHIPS );
file_stream = fopen("ships.dat", "w");
display_ship_info( ships, tracking, NO_OF_SHIPS, file_stream, INITIAL_SHIP_INFO );
for (i=0; i<SHIP_HOURS; i++)
move_ships( ships, tracking, NO_OF_SHIPS );
display_ship_info( ships, tracking, NO_OF_SHIPS, file_stream, FINAL_SHIP_INFO );
fclose(file_stream);
}
/* ------------------------------------------------------------------------------- */
static void initialize_ships( ship_type ships[], char tracking[], int no_of_ships )
{
int i;
/*
Initialize the tracking array to track all ships. Set the hull number by the
given formula, and get position, course and speed of each ship by random function.
*/
for (i=0; i<no_of_ships; i++)
{
tracking[i] = TRACKING;
ships[i].hull_number = (i + 1) * 10;
ships[i].posit.longitude = (double) random(MIN_POSITION, MAX_POSITION);
ships[i].posit.latitude = (double) random(MIN_POSITION, MAX_POSITION);
ships[i].course = (double) random(MIN_COURSE, MAX_COURSE);
ships[i].speed = (double) random(MIN_SPEED, MAX_SPEED);
}
}
/* ------------------------------------------------------------------------------- */
static void move_ships( ship_type ships[], char tracking[], int no_of_ships )
{
int i;
for (i=0; i<no_of_ships; i++)
if (tracking[i]) /* if ship still being tracked, get new position */
{
find_new_position( ships, i );
if (!(check_if_still_track( ships, i ))) /* check if ship in boundaries */
tracking[i] = NOT_TRACKING; /* turn tracking of ship off if not in boundaries */
}
}
/* ------------------------------------------------------------------------------- */
static void find_new_position( ship_type ships[], int sub )
{
/*
Since course starts at north (0 degrees), and increases clockwise,
the sine of the angle multiplied by distance will give the longitude
component, while the cosine gives the latitude distance.
*/
ships[sub].posit.longitude += (sin(ships[sub].course * RADIANS_PER_DEGREE) *
ships[sub].speed);
ships[sub].posit.latitude += (cos(ships[sub].course * RADIANS_PER_DEGREE) *
ships[sub].speed);
}
/* ------------------------------------------------------------------------------- */
static int check_if_still_track( ship_type ships[], int sub )
{
/*
Check to see if longitude or latitude is outside of boundaries. If so, return
a NO_TRACKING, else return TRACKING.
*/
if ((ships[sub].posit.longitude < (double) MIN_POSITION) ||
(ships[sub].posit.longitude > (double) MAX_POSITION) ||
(ships[sub].posit.latitude < (double) MIN_POSITION) ||
(ships[sub].posit.latitude > (double) MAX_POSITION))
return NOT_TRACKING;
else
return TRACKING;
}
/* ------------------------------------------------------------------------------- */
static void display_ship_info( ship_type ships[], char tracking[],
int no_of_ships, FILE *file_stream, char output_type )
{
int i;
/*
The output of both initial and final data uses this procedure. The parameter
output_type tells the computer what title to give each output set.
*/
if (output_type)
fprintf(file_stream, "Initial Ship Information:\n\n");
else
fprintf(file_stream, "\n\n\nFinal Ship Information:\n\n");
fprintf(file_stream, "Ship Hull Number Longitude Latitude Course Speed\n");
for (i=0; i<no_of_ships; i++)
if (tracking[i]) /* print only if still in boundaries */
{
fprintf(file_stream, "%4d%9d%13.2lf%11.2lf%8.0lf%7.0lf\n", i,
ships[i].hull_number, ships[i].posit.longitude, ships[i].posit.latitude,
ships[i].course, ships[i].speed);
}
}